local skel = fk.CreateSkill {
  name = "rmt__yanfeng",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player.phase == Player.Play then
      local room = player.room
      local id = 0
      for _, move in ipairs(data) do
        if move.from == nil and move.moveReason == fk.ReasonUse then
          local move_event = room.logic:getCurrentEvent()
          local use_event = move_event.parent
          if use_event ~= nil and use_event.event == GameEvent.UseCard then
            local use = use_event.data
            if use.from == player and use.card.trueName == "slash" and not use.card:isVirtual() and
              (player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 or
              (use.extra_data and use.extra_data.rmt__yanfeng == player.id)) and
              table.contains(room.discard_pile, use.card.id) then
              for _, info in ipairs(move.moveInfo) do
                if info.cardId == use.card.id then
                  id = use.card.id
                end
              end
            end
          end
        end
      end
      if id == 0 then return end
      local cards = player.room.logic:moveCardsHoldingAreaCheck({id})
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local use = player.room:askToUseRealCard(player, {
      pattern = event:getCostData(self).cards,
      skill_name = self.name,
      prompt = "#rmt__yanfeng-use:::"..Fk:getCardById(event:getCostData(self).cards[1]):toLogString()..":"..
      (2 - player:usedSkillTimes(self.name, Player.HistoryPhase)),
      extra_data = {
        bypass_times = true,
        extraUse = true,
        expand_pile = event:getCostData(self).cards,
      },
      skip = true,
      cancelable = true,
    })
    if use then
      event:setCostData(self, {extra_data = use})
      return true
    elseif player:usedSkillTimes(self.name, Player.HistoryPhase) > 0 then
      player.room:setPlayerMark(player, "@rmt__yanfeng", 3)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self).extra_data
    if player:usedSkillTimes(self.name, Player.HistoryPhase) == 1 then
      room:setPlayerMark(player, "@rmt__yanfeng", 0)
      use.extra_data = use.extra_data or {}
      use.extra_data.rmt__yanfeng = player.id
    end
    room:useCard(use)
    if player.dead then return end
    if player:usedSkillTimes(self.name, Player.HistoryPhase) > 1 then
      room:setPlayerMark(player, "@rmt__yanfeng", 3)
    end
  end,
})

skel:addEffect(fk.DamageInflicted, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@rmt__yanfeng") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("rmt__yanfeng")
    if player:getMark("@rmt__yanfeng") == 3 then
      room:notifySkillInvoked(player, "rmt__yanfeng", "negative")
      data.damage = 2
    elseif player:getMark("@rmt__yanfeng") == 2 then
      room:notifySkillInvoked(player, "rmt__yanfeng", "defensive")
      data.damage = 0
    elseif player:getMark("@rmt__yanfeng") == 1 then
      room:notifySkillInvoked(player, "rmt__yanfeng", "negative")
      data.damage = 3
    end
    room:removePlayerMark(player, "@rmt__yanfeng", 1)
  end,
})

Fk:loadTranslationTable{
  ["rmt__yanfeng"] = "焱锋",
  [":rmt__yanfeng"] = "出牌阶段限一次，你可以将一张【杀】重复使用三次（在此【杀】进入弃牌堆后继续使用，仅第一次计入次数），然后直到"..
  "你下次发动此技能，你受到的前三次伤害依次改为2，0，3。",
  ["#rmt__yanfeng-use"] = "焱锋：你可以重复使用%arg三次！（还剩%arg2次）",
  ["@rmt__yanfeng"] = "焱锋",

  ["$rmt__yanfeng1"] = "破敌何殊逐雉兔？",
  ["$rmt__yanfeng2"] = "吞虏气填膺，报君忠贯日！",
}

return skel
